import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class CallableThreadDemo implements Callable<String> {
  @Override
  public String call() throws Exception {
    int i = 0;
    for (; i < 10; i++) {
      System.out.println(Thread.currentThread().getName() + " " + i);
    }
    return "Data returned by thread: "+ Thread.currentThread().getName();
  }
}

public class RunnableDemo {
  public static void main(String[] args) throws InterruptedException, ExecutionException {
    // CallableThreadDemo ctt = new CallableThreadDemo();
    FutureTask<String> ft = new FutureTask<String>(new CallableThreadDemo());

    for (int i = 0; i < 10; i++) {
      System.out.println(Thread.currentThread().getName() + " 的循环变量i的值" + i);
      if (i == 2 || i == 4) {
        new Thread(ft, "CallableThreadDemotest的线程:"+i).start();
        System.out.println("子线程的返回值：" + ft.get());
      }
    }

  }
}

/*  
main 的循环变量i的值0
main 的循环变量i的值1
main 的循环变量i的值2
CallableThreadDemotest的线程:2 0
CallableThreadDemotest的线程:2 1
CallableThreadDemotest的线程:2 2
CallableThreadDemotest的线程:2 3
CallableThreadDemotest的线程:2 4
CallableThreadDemotest的线程:2 5
CallableThreadDemotest的线程:2 6
CallableThreadDemotest的线程:2 7
CallableThreadDemotest的线程:2 8
CallableThreadDemotest的线程:2 9
子线程的返回值：Data returned by thread: CallableThreadDemotest的线程:2
main 的循环变量i的值3
main 的循环变量i的值4
子线程的返回值：Data returned by thread: CallableThreadDemotest的线程:2
main 的循环变量i的值5
main 的循环变量i的值6
main 的循环变量i的值7
main 的循环变量i的值8
main 的循环变量i的值9
*/